package rjgc.mall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rjgc.mall.common.MessageConstant;
import rjgc.mall.common.MySessionContext;
import rjgc.mall.common.Result;
import rjgc.mall.entity.User;
import rjgc.mall.entity.vo.Order;
import rjgc.mall.entity.vo.UserHistoryOrders;
import rjgc.mall.mapper.TradeActionMapper;
import rjgc.mall.service.OrderService;
import rjgc.mall.service.SellerService;
import rjgc.mall.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private TradeActionMapper tradeActionMapper;
    /**
     *
     * @param id
     * @return
     */
    @RequestMapping("/getOrder")
    public Result getOrder(Integer id){
        Order order = orderService.getOrder(id);
        return new Result(true, MessageConstant.SELECT_ORDER,order);
    }

    /**
     * 买家查看自己的订单历史以及订单详情
     * @param userToken
     * @return
     */
    @RequestMapping("/viewOrders")
    public Result viewOrders(@RequestHeader("userToken") String userToken){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        User user=(User) session.getAttribute("user");
        UserHistoryOrders result = orderService.viewOrders(user.getId());
        return new Result(true,MessageConstant.USER_VIEWORDERS_SUCCESS,result);
    }

    //此后为本学期新增
    /************取消订单**********/
    @RequestMapping("/CancelTrad")
    public Result CancelTrad(Integer id, Integer goodsid){
        System.out.println("成功");
        userService.CancelTrade(id);
        Integer stock=sellerService.inquireStock(goodsid);
        if(stock==0){
            sellerService.UpGoods(goodsid);
        }
        sellerService.AddStock(goodsid);
        return new Result<>(true,"取消成功");
    }

    /************将订单状态从已发货变更为交易完成**********/
    @RequestMapping("/sendOutwaitingForReception")
    public Integer SendOutwatingForReception(Integer tradeid){
        return orderService.sendOutwatingForReception(tradeid);
    }
    /************根据订单编号将订单状态从已发货变更为交易完成**********/
    @RequestMapping("/sendOutwaitingForReceptionByTradeNo")
    public Integer SendOutwatingForReceptionByTradeNo(String tradeNo){
        return tradeActionMapper.sendOutwatingForReceptionByTradeNo(tradeNo);
    }
    /************根据订单id取消订单**********/
    @RequestMapping("/CancelTrad2")
    public Result CancelTrad2(Integer id, Integer goodsid){
        System.out.println("成功");
        int num = orderService.getTradeNum(id);
        Integer stock=sellerService.inquireStock(goodsid);
        if(stock==0){
            sellerService.UpGoods(goodsid);
        }
        userService.CancelTrade(id);
        sellerService.AddStock2(goodsid,num);
        return new Result<>(true,"取消成功");
    }
    /************根据订单编号取消订单**********/
    @RequestMapping("/CancelTradByTradeNo")
    public Result CancelTradByTradeNo(String tradeNo){
        List<Integer> tradeidList = tradeActionMapper.getTradeIdByTradeNo(tradeNo);
        //System.out.println(tradeidList);
        List<Integer> goodidList = tradeActionMapper.getGoodIdByTradeNo(tradeNo);
        //System.out.println(goodidList);
        for (int i=0;i<tradeidList.size();i++){
            int tradeid = tradeidList.get(i);
            int goodid = goodidList.get(i);
            int num = orderService.getTradeNum(tradeid);
            int stock =sellerService.inquireStock(goodid);
            if(stock==0){
                sellerService.UpGoods(goodid);
            }
            userService.CancelTrade(tradeid);
            sellerService.AddStock2(goodid,num);
        }
        return new Result<>(true,"取消成功");
    }
}
